from django import forms

from users.models import UserProfile


class RegisterPostForm(forms.Form):
    username = forms.CharField(required=True)
    password = forms.CharField(required=True, min_length=6)

    def clean_username(self):
        username = self.data.get("username")
        # 验证用户名是否已注册
        users = UserProfile.objects.filter(username=username)
        if users:
            raise forms.ValidationError("该用户名已注册")
        return username


class LoginForm(forms.Form):
    username = forms.CharField(required=True)
    password = forms.CharField(required=True, min_length=6)
